package com.sgb.kjwl.view.ui.adapter.pricecheck;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.pricecheck.SupplierEntitiy;
import com.sgb.kjwl.utils.DisplayUtil;
import com.sgb.kjwl.view.ui.activity.priceCheck.FindSupplierActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.util.List;

/**
 * @author by 2021.03.13
 */
public class FindSupplierAdapter extends BaseRecycleAdapter<SupplierEntitiy> {

    private int selectCount = 0;
    private static final int MAX_COUNT = 10;
    private Drawable drawableVip;
    private int drawablePadding;
    private OnSupplierItemClickListener mSupplierClickListener;


    public FindSupplierAdapter(Context context, List<SupplierEntitiy> datas) {
        super(context, R.layout.item_find_supplier, datas);
        initVipDrawable();
    }

    private void initVipDrawable() {
        this.drawableVip = ContextCompat.getDrawable(context, R.mipmap.ic_vip);
        this.drawableVip.setBounds(0, 0, DisplayUtil.convertDpToPixel(context, 18), DisplayUtil.convertDpToPixel(context, 15));
        this.drawablePadding = DisplayUtil.convertDpToPixel(context, 5);
    }

    @Override
    public void convert(ItemViewHolder holder, SupplierEntitiy supplierEntitiy) {
        holder.setBinding(BR.viewModel, supplierEntitiy);
        TextView tvCompany = holder.itemView.findViewById(R.id.companyName);
        showCompanyVipIcon(tvCompany, supplierEntitiy);
        ImageView itemSelectOption = holder.itemView.findViewById(R.id.itemSelectOption);
        /***
         * 修复1.3.6版本的BUG
         */
        setItemImageResource(itemSelectOption, supplierEntitiy.isSelected());
        LinearLayout supplierItemView = holder.itemView.findViewById(R.id.ll_supplierItem);
        supplierItemView.setOnClickListener(v -> {
            if (this.selectCount >= MAX_COUNT) {
                MToast.showToast(context, "最多添加10家供应商");
                return;
            }
            supplierEntitiy.setSelected(supplierEntitiy.isSelected() ? false : true);
            setItemImageResource(itemSelectOption, supplierEntitiy.isSelected());
            mSupplierClickListener.onClickSupplierItem(supplierEntitiy);
        });
    }

    /***
     * 当前被选中的供应商个数
     * @param selectCount
     */
    public void setSelectCount(int selectCount) {
        this.selectCount = selectCount;
    }

    /***
     * 设置单个ITEM的状态
     * @param imageView
     * @param isSelected
     */
    private void setItemImageResource(ImageView imageView, boolean isSelected) {
        if (isSelected) {
            imageView.setImageResource(R.mipmap.gouxuan);
        } else {
            imageView.setImageResource(R.mipmap.weixuanzhong);
        }
    }

    private void showCompanyVipIcon(TextView tvCompany, SupplierEntitiy entitiy) {
        if (entitiy.getFlag() == 1) {
            tvCompany.setCompoundDrawables(null, null, drawableVip, null);
            tvCompany.setCompoundDrawablePadding(drawablePadding);
        } else {
            tvCompany.setCompoundDrawables(null, null, null, null);
        }

    }

    /***
     *     删除的回调
     */
    public interface OnSupplierItemClickListener {
        /***
         *单个供应商item的点击事件
         * @param supplierEntitiy
         */
        void onClickSupplierItem(SupplierEntitiy supplierEntitiy);
    }

    /***
     *
     * @param listener
     */
    public void setOnItemClickListener(OnSupplierItemClickListener listener) {
        mSupplierClickListener = listener;
    }
}
